﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 选择类别对话框的基类
/// </summary>
public abstract class PSControls : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //绑定栏目下拉
            ListControl_DataBind();
            if (classSelected != string.Empty)
            {
                GetClassControl.SelectedValue = classSelected;
            }
            //激活脚本事件
            if (isactivScript)
            {
                ScriptEvent();
            }
            //激活服务器事件
            if (isactivServer)
            {
                GetClassControl.AutoPostBack = true;
            }
        }
    }

    #region 成员    
    string requestPath = string.Empty;    
    bool isactivScript = false;
    bool isactivServer = false;
    string scriptAction = string.Empty;
    string classSelected = string.Empty;
    #endregion

    #region 属性    
    /// <summary>
    /// 设置请求页面地址
    /// </summary>
    public string RequestPath
    {
        set { requestPath = value; }
    }    
    /// <summary>
    /// 是否激活脚本事件
    /// </summary>
    public bool IsActivationScript
    {
        set { isactivScript = value; }
    }
    /// <summary>
    /// 是否激活服务器事件
    /// </summary>
    public bool IsActivationServer
    {
        set { isactivServer = value; }
    }    
    /// <summary>
    /// 设置脚本动作内容
    /// </summary>
    public string ScriptAction
    {
        set { scriptAction = value; }
    }
    /// <summary>
    /// 获取或设置类别选定项
    /// </summary>
    public string ClassSelected
    {
        set { classSelected = value; }
        get { return GetClassControl.SelectedValue; }
    }
    #endregion

    #region 方法
    /// <summary>
    /// 脚本事件
    /// </summary>
    void ScriptEvent()
    {
        //设置脚本
        GetClassControl.Attributes["onchange"] = (scriptAction == string.Empty) ? "location = '" + requestPath +
            "?classid=' + this.value;" : scriptAction;
    }
    #endregion

    #region 抽象属性
    /// <summary>
    /// 获取下拉列表客户端名称
    /// </summary>
    public abstract string Client_ClassID
    {
        get;
    }
    /// <summary>
    /// 获取栏目集合控件
    /// </summary>
    public abstract ListControl GetClassControl
    {
        get;
    }
    #endregion

    #region 抽象方法
    /// <summary>
    /// 绑定下拉列表数据
    /// </summary>
    public abstract void ListControl_DataBind();
    #endregion

    #region 委托事件
    /// <summary>
    /// 委托的事件句柄
    /// </summary>
    public event System.EventHandler Class_OnChange;
    /// <summary>
    /// 委托事件的附加属性
    /// </summary>
    public EventArgs Class_EventArgs;
    #endregion

    #region 注册委托
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        GetClassControl.SelectedIndexChanged += new EventHandler(GetClassControl_SelectedIndexChanged);
    }

    void GetClassControl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Class_OnChange != null)
        {
            Class_OnChange(GetClassControl, null);
        }
    }
    #endregion
}
